﻿Imports System.Drawing
Imports ajWebClient = ajayumi.develop.VBasic.Win.Controls.WebClient
Imports System.IO
Imports System.ComponentModel
Imports ajayumi.develop.win32.WebResourcesCatcher.Service
Imports System.Threading
Imports System.Net
Imports ajayumi.develop.VBasic.Core

Public Class DgvImages
    Inherits DgvResourceBase

#Region "Variables"
    Private m_RowNum As Long = 0L   '行号

#End Region

#Region "Properties"


#End Region

    Protected Overloads Overrides Sub LoadItems(lst As System.Collections.Generic.List(Of WebResourceInfo))
        Try
            Dim _Size As New Size(0, 0)
            Dim _ErrorMsg As String = String.Empty
            Dim _Total As Integer = lst.Count
            Dim _Index As Integer = 0
            Dim _ProgressPercentage As Integer = 0
            Dim _ResDownloaderList As New List(Of ResourceDownloader)()

            For Each item As WebResourceInfo In lst
                _Index += 1
                Dim _WebResourceInfo As WebResourceInfo = item

                Me.m_StopWatch = Stopwatch.StartNew()
                Using _WebClient As New WebClient()
                    Try
                        Using _Stream As Stream = _WebClient.OpenRead(_WebResourceInfo.Url)
                            Using img As Image = Image.FromStream(_Stream)
                                _Size = img.Size
                            End Using
                        End Using
                    Catch ex As Exception
                        _ErrorMsg = ex.Message
                    End Try
                End Using
                Me.m_StopWatch.Stop()

                Dim _isMatch As Boolean = False

                Select Case Me.ResTaskInfo.MatchMode
                    Case MatchMode.AND
                        If _Size.IsEmpty = False AndAlso (_Size.Width > Me.ResTaskInfo.ImgWidthLB AndAlso _Size.Height > Me.ResTaskInfo.ImgHeightLB) Then
                            _isMatch = True
                            Console.ForegroundColor = ConsoleColor.Green
                            Console.WriteLine("{0}/{1}  {2} 符合条件，耗时：{3}毫秒", _Index, _Total, _WebResourceInfo.Url, Me.m_StopWatch.ElapsedMilliseconds)
                            Console.ForegroundColor = ConsoleColor.White
                        Else
                            Console.WriteLine("{0}/{1}  {2} 不符合条件，耗时：{3}毫秒", _Index, _Total, _WebResourceInfo.Url, Me.m_StopWatch.ElapsedMilliseconds)
                        End If
                    Case MatchMode.OR
                        If _Size.IsEmpty = False AndAlso (_Size.Width > Me.ResTaskInfo.ImgWidthLB OrElse _Size.Height > Me.ResTaskInfo.ImgHeightLB) Then
                            _isMatch = True
                            Console.ForegroundColor = ConsoleColor.Green
                            Console.WriteLine("{0}/{1}  {2} 符合条件，耗时：{3}毫秒", _Index, _Total, _WebResourceInfo.Url, Me.m_StopWatch.ElapsedMilliseconds)
                            Console.ForegroundColor = ConsoleColor.White
                        Else
                            Console.WriteLine("{0}/{1}  {2} 不符合条件，耗时：{3}毫秒", _Index, _Total, _WebResourceInfo.Url, Me.m_StopWatch.ElapsedMilliseconds)
                        End If
                End Select

                If _isMatch Then
                    Me.m_RowNum += 1
                    Dim _SavePath As String = String.Concat(Me.ResTaskInfo.SaveDir, _
                        StringHelper.FilterInvalidChars(item.Title), "_", _
                        StringHelper.FilterInvalidChars(System.IO.Path.GetFileName(item.Url)))

                    With _WebResourceInfo
                        .RowNum = Me.m_RowNum
                        .Url = item.Url
                        .SavePath = _SavePath
                        .ProgressPercentage = 0
                        .ResStatus = ResourceStatus.Waiting
                        .Size = _Size.ToString()
                        .Information = String.Empty
                    End With


                    Dim _Downloader As New ResourceDownloader(_WebResourceInfo)
                    _ResDownloaderList.Add(_Downloader)
                    Me.Invoke(New Action(Of WebResourceInfo)(AddressOf Me.m_WebResourceCollection.Add), _
                              New Object() {_WebResourceInfo})
                End If

                _ProgressPercentage = CInt(CDbl(_Index) / CDbl(_Total) * 100.0#)
                MyBase.OnLoadProgress(New ProgressChangedEventArgs(_ProgressPercentage, Me))
            Next

            _ResDownloaderList.ForEach(Sub(res As ResourceDownloader)
                                           res.Start()
                                       End Sub)
        Catch ex As Exception
            MyBase.OnLoadComplete(New System.ComponentModel.AsyncCompletedEventArgs(ex, True, Me))
        Finally

        End Try
    End Sub

    ''' <summary>
    ''' 下载勾选项
    ''' </summary>
    ''' <remarks></remarks>
    Sub DownloadCheckedItems()
        Dim _List As List(Of WebResourceInfo) = Me.GetCheckedItems(Of WebResourceInfo)(Me, 0)

        _List.ForEach(Sub(item As WebResourceInfo)
                          Using _Downloader As New ResourceDownloader(item)
                              _Downloader.Start()
                          End Using
                      End Sub)
    End Sub


End Class
